<?php

require_once("configure.php");

$category = $_POST["category"];
$user = $_POST["user"];

$MySqlLinkId = mysql_connect(DBHost, DBUser, DBPwd);
if($MySqlLinkId == FALSE)
{
	echo("({ErrCode:1, ErrMsg:'链接数据库失败！'})");
	exit();
}

if(FALSE == mysql_select_db(DB, $MySqlLinkId))
{
	echo("({ErrCode:3, ErrMsg:'".json_encode(mysql_error($MySqlLinkId))."'})");
	mysql_close($MySqlLinkId);
	exit();
}

$BillData = Array();
$sql = sprintf("SELECT Bill.Id, PayAmount, Name, Remark, CreateTime FROM Bill INNER JOIN BillCategory ON Bill.CategoryId = BillCategory.Id WHERE Bill.AccountName='%s' AND CreateTime>='%s' %s ORDER BY CreateTime ASC", $user, date('Y-m-01', (time() - (12 * 30 * 24 * 60 * 60))), $category>0?"AND CategoryId=$category":"");
$result = mysql_query($sql, $MySqlLinkId);
while(($row = mysql_fetch_row($result)))
{
	$BillData[] = Array($row[0], ((float)$row[1]) / 100, $row[2], $row[3], $row[4]);
}
mysql_free_result($result);
mysql_close($MySqlLinkId);

$BillMonth = Array();
$now = strptime(date("Y-m-d"), "%Y-%m-%d");
foreach($BillData as $d)
{
	$datetime = strptime($d[4], "%Y-%m-%d");
	$BillMonth[sprintf("%04d-%02d", ($datetime["tm_year"]+1900), ($datetime["tm_mon"]+1))] += $d[1];
}

$chart1_data = Array();
foreach($BillMonth as $k => $bm)
{
	$chart1_data[] = Array($k, $bm);
}
echo("({ErrCode:0, ErrMsg:'', xml:".json_encode($chart1_data)."})");

?>